<?php
require_once FOREGROUND_COMMON_ACTION_SERVICE_DIR . '/ajax.php';

class foreground_cart_change_number_action_service extends foreground_ajax_common_action_service
{
    protected static $_instance;

    public static function get_instance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }


    protected function _current_page_process()
    {	     		     
	    $result = array('status' => 0, 'message' => '', 'data' => array());
	
	    //var_dump($_POST); exit();
	    $id = init_user_data_key($_POST, 'id', 'int');
	    $good_number = init_user_data_key($_POST, 'good_number', 'int');
	
	    $action_data = wwy_action::$data;
	    $session_member_info = $action_data['session_member_info'];
	    $member_id = $session_member_info['id'];
		         
        if(empty($id)){
            $result['status']  = -2;
            $result['message'] = '修改失败';
        }else if($good_number < 1){
            $result['status']  = -3;
            $result['message'] = '修改失败';
        }
        
        if($result['status'] == 0){            
            $model_foreground_cart = wwy_resource_container::get_model_foreground_cart();
            //var_dump($model_foreground_cart); exit();
                                                       
            $where      = array(
                'id'        => $id,
                'member_id' => $member_id,
            );
            $cart_row = $model_foreground_cart->fetch_row($where);
            //var_dump($cart_row); exit();
        
            if(empty($cart_row)){
                $result['status']  = -4;
                $result['message'] = '修改失败';
            }else{ 
                //1>检查当前商品规格是否存在，若存在，即未下架，则继续操作；否则提示【当前商品规格已下架，将重新加载页面，清除已下架的商品规格】，然后重新加载页面，清除已下架的商品规格
                //2>检查是否存在已下架的商品规格，若存在，则提示【修改成功。您的购物车存在已下架的商品规格，将重新加载页面，清除已下架的商品规格】，然后重新加载页面，清除已下架的商品规格
                
                $good_id = $cart_row['good_id'];
                $spec_id = $cart_row['spec_id'];
                $check_good_spec_result = $model_foreground_cart->check_good_spec($good_id, $spec_id);
                //var_dump($check_good_spec_result); exit();
                if($check_good_spec_result['status'] < 0){
                    $result['status']  = -404;
                    $result['message'] = '修改失败。当前商品规格已下架，将重新加载页面，清除已下架的商品规格';                                           
                }else{  
                    $good_info = $check_good_spec_result['data']['good_info'];                        
                    
                    $origin_good_number = $cart_row['good_number'];
                    if($good_number == $origin_good_number){
                        $result['status']  = -5;
                        $result['message'] = '修改失败';
                    }else{                                                                                                                                                                                                        
                        if($result['status'] == 0){
                            $model_foreground_good = wwy_resource_container::get_model_foreground_good();
                            
                            $match_spec_number_grade_price = $model_foreground_good->get_match_spec_number_grade_price($good_id, $spec_id, $good_number);
                            if(empty($match_spec_number_grade_price)){
                                $result['status']  = -400;
                                $result['message'] = '加入购物车失败。该商品规格购物车数量更新为' . $good_number . '，没有匹配的数量范围';
                            }else{  
                                $good_price = $match_spec_number_grade_price['price'];
                                                                                                      
                                $origin_good_price = $cart_row['good_price'];
                                $origin_item_total = $origin_good_price * $origin_good_number;
                                $new_item_total    = $good_price * $good_number;
                                $change_item_total = $new_item_total - $origin_item_total;
                    
                                $cart_row_options = array(
                                    'id'          => $cart_row['id'],
                                    'good_number' => $good_number,
                                    'good_price'  => $good_price,
                                );
                                $save_result = $model_foreground_cart->update_good_number($cart_row_options);
            	                //var_dump($save_result); exit();
                                if(!$save_result){
                                    $result['status']  = -6;
                                    $result['message'] = '修改失败';
                                }else{
                                    $check_cart_list_good_spec_result = $model_foreground_cart->check_cart_list_good_spec($member_id);
                                    if($check_cart_list_good_spec_result['status'] < 0){
                                        //检查是否存在已下架的商品规格，若存在，则提示【修改成功。您的购物车存在已下架的商品规格，将重新加载页面，清除已下架的商品规格】，然后重新加载页面，清除已下架的商品规格                                        
                                        $result['status']  = -404;
                                        $result['message'] = '修改成功。您的购物车存在已下架的商品规格，将重新加载页面，清除已下架的商品规格';
                                    }else{                                                                                                            
                                        $result['status']  = 1;
                                        $result['message'] = '修改成功';
                                        $result['data']['new_item_good_price'] = $good_price;
                                        $result['data']['new_item_total']    = number_format($new_item_total, 2, '.', '');
                                        $result['data']['change_item_total'] = number_format($change_item_total, 2, '.', '');
                                        
                                        self::add_view_data('new_item_good_price', $result['data']['new_item_good_price']);
                                        self::add_view_data('new_item_total', $result['data']['new_item_total']);
                                        self::add_view_data('change_item_total', $result['data']['change_item_total']);  
                                    }
                                }
                            }
                        }                            
                    }
                }
                
            }
        }

	    
        self::set_view_status($result['status']);
	    self::set_view_message($result['message']);
    }
}